//
//  BaseViewController.m
//  BossSystem
//
//  Created by hzyd-wlj on 14-6-23.
//  Copyright (c) 2014年 hzyd-wlj. All rights reserved.
//

#import "BaseViewController.h"
#import "AppDelegate.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

@interface BaseViewController ()
@property(nonatomic,copy) void (^leftBarActionBlock)(void);
@property(nonatomic,copy) void (^rightBarActionBlock)(void);
@end

@implementation BaseViewController

- (NSString *)userID
{
    NSString *userID = [[NSUserDefaults standardUserDefaults]objectForKey:KUserDefaultsUserIdKey];
    if(userID == nil)
    {
        userID = @"1";
    }
    [[NSUserDefaults standardUserDefaults]synchronize];
    return userID;
}

//- (UserInformation *)currentUser
//{
//    NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:KUserDefaultsUserInformationKey];
//    UserInformation *user = [NSKeyedUnarchiver unarchiveObjectWithData:data];
//    return user;
//}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //为了解决在自定义NavBar以后，ios7手势返回失效的问题，
    [self.navigationController.interactivePopGestureRecognizer setDelegate:nil];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.071 green:0.075 blue:0.078 alpha:1.000]; //bar背景颜色
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    
};

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 数据相关

//- (User *)currentUser
//{
//    NSData *userData = [KStandUserDefaults objectForKey:KUserDefaultKey_User];
//    User *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
//    return user;
//}
//
//- (void)changeCurrentUser:(User *)currentUser
//{
//    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:currentUser];
//    [KStandUserDefaults setObject:userData forKey:KUserDefaultKey_User];
//    [KStandUserDefaults synchronize];
//}
//
//- (void)changeUserUniversity:(University *)university
//{
//    User *userInfo = self.currentUser;
//    userInfo.universityId = @([university.objectId intValue]);
//    userInfo.userName = university.name;
//    
//    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:userInfo];
//    [KStandUserDefaults setObject:userData forKey:KUserDefaultKey_User];
//    [KStandUserDefaults synchronize];
//}

#pragma mark -导航栏相关
- (void)addBackButton
{
    __weak typeof(self)weakSelf = self;
    [self addLeftButtonWithImage:[UIImage imageNamed:@"返回箭头"] actionBlock:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    self.navigationItem.backBarButtonItem.style = UITableViewRowActionStyleDestructive;
}

- (void)addLeftButtonWithImage:(UIImage *)image actionBlock:(void(^)(void))block
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 24, 24)];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [leftButton addTarget:self action:@selector(leftBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.leftBarActionBlock = block;
    self.navigationItem.leftBarButtonItem = barButtonItem;
}
- (void)leftBar_Touched:(id)sender
{
    if (self.leftBarActionBlock) {
        self.leftBarActionBlock();
    }
}

- (void)addRightButtonWithImage:(UIImage *)image actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 49, 9)];
    [rightButton setImageEdgeInsets:UIEdgeInsetsMake(-15, 0, 0, -15)];
    [rightButton setImage:image forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)addRightButtonWithTitle:(NSString *)title actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [rightButton setFrame:CGRectMake(0, 0, 60, 30)];
    rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -20)];
    [rightButton setTitleColor:[UIColor colorWithRed:0.000 green:0.467 blue:0.984 alpha:1.000] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)rightBar_Touched:(id)sender
{
    if (self.rightBarActionBlock) {
        self.rightBarActionBlock();
    }
}

- (void)setUpNavgationBar
{
    NSAssert(NO, @"需要子类重载");
}

#pragma mark - 网络相关


#pragma mark - CoreData操作
- (NSArray *)fetchEntityForName:(NSString *)entityName inDefaultMOCWithPredicateFormat:(NSString *)predicateString,...
{
    return [self fetchEntityForName:entityName inManagedObjectContext:self.defaultObjectContext WithPredicateFormat:predicateString];
}

- (NSArray *)fetchEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)moc WithPredicateFormat:(NSString *)predicateString, ...
{
//    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:moc];
//    [fetchRequest setEntity:entity];
//    if (predicateString&&predicateString.length>0) {
//        // Specify criteria for filtering which objects to fetch
//        NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
//        [fetchRequest setPredicate:predicate];
//    }
//    // Specify how the fetched objects should be sorted
//    
//    NSError *error = nil;
//    NSArray *result = [moc executeFetchRequest:fetchRequest error:&error];
//    if (error) {
//        DLog(@"%@",[error debugDescription]);
//    }
    return nil;
}

//- (NSManagedObjectContext *)defaultObjectContext
//{
//    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
//    return  appDelegate.managedObjectContext;
//}

//- (BOOL)saveInDefaultObjectContext
//{
//    NSError *error;
//    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
//    BOOL isSucess = [appDelegate.managedObjectContext save:&error];
//    if (!isSucess) {
//        DLog(@"CoreData插入失败:%@,%@",error,[error userInfo]);
//    }
//    return isSucess;
//}

//- (NSManagedObjectContext *)newBackgroundObjectContext
//{
//    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
//    return [appDelegate newBackgroundManagedObjectContext];
//}
@end
